package com.ce.server.controller;


import com.ce.server.common.RespBean;
import com.ce.server.common.RespPageBean;
import com.ce.server.pojo.Admin;
import com.ce.server.pojo.Checkin;
import com.ce.server.pojo.Operlog;
import com.ce.server.service.ICheckinService;
import com.ce.server.service.IOperlogService;
import com.ce.server.service.IScheduleService;
import com.ce.server.service.IStudentService;
import com.ce.server.util.AdminUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/checkin")
public class CheckinController {

    @Autowired
    ICheckinService checkinService;
    @Autowired
    IStudentService studentService;
    @Autowired
    IScheduleService scheduleService;
    @Autowired
    private IOperlogService operlogService;
    @Value("${type.student}")
    private String type;

    @ApiOperation(value = "根据id删除某条入校记录")
    @DeleteMapping("/")
    public RespBean deleteCheckInById(Integer[] ids) {
        Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
        operlogService.save(new Operlog().setWno(currentAdmin.getWno())
                .setWname(currentAdmin.getWname())
                .setOper("根据id删除了某条入校记录").setOpertime(LocalDateTime.now()));
        if (checkinService.removeByIds(Arrays.asList(ids))) return RespBean.success("删除成功!");
        return RespBean.error("删除失败!");
    }

    @ApiOperation(value = "新增入校记录或核酸记录并更新学生的健康码信息")
    @PostMapping("/{sno}/{jkm_status}")
    public RespBean addCheckInBySno(@PathVariable("sno") String sno,@PathVariable("jkm_status") String inputJkmStatus) {
        Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
        operlogService.save(new Operlog().setWno(currentAdmin.getWno())
                .setWname(currentAdmin.getWname())
                .setOper("新增入校记录或核酸记录并更新学生的健康码信息").setOpertime(LocalDateTime.now()));
        return checkinService.autoFillCheckIn(sno,inputJkmStatus);
    }

    @ApiOperation(value = "查询所有记录(分页处理)")
    @GetMapping("/")
    public RespPageBean getAllRecords(@RequestParam(defaultValue = "1") Integer currentPage,
                                      @RequestParam(defaultValue = "10") Integer size,
                                      Checkin checkin, LocalDate[] beginDateScope){
        return checkinService.getAllRecords(currentPage,size,checkin,beginDateScope);
    }



}
